package com.goblin.model.film.repository;

import javax.persistence.EntityManager;

import com.goblin.common.response.PageHelper;
import com.goblin.common.response.Paged;
import com.goblin.model.base.BaseRepositoryImpl;
import com.goblin.model.film.po.Comment;
import com.goblin.model.film.po.QComment;
import com.querydsl.core.QueryResults;
import com.querydsl.jpa.JPQLQuery;

public class CommentRepositoryImpl extends BaseRepositoryImpl<Comment, Integer>
		implements CommentRepositoryCustom {

	public CommentRepositoryImpl(EntityManager entityManager) {
		super(Comment.class, entityManager);
	}

	@Override
	public Paged<Comment> paged(PageHelper pageable, Integer id) {
		JPQLQuery<Comment> query = this.jpaQueryFactory.selectFrom(QComment.comment)
				.where(QComment.comment.deleteTime.isNull())
				.where(QComment.comment.film.id.eq(id))
				.orderBy(QComment.comment.likes.desc());
		query = (JPQLQuery<Comment>) getQuerydsl(pageable, query);
		QueryResults<Comment> results = query.fetchResults();
		return new Paged<Comment>(results.getTotal(), results.getResults());
	}
}
